<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>对象原型</title>
    <script>
      // 函数的特殊属性: prototype
      log(
        "JavaScript 常被描述为一种基于原型的语言 (prototype-based language)——每个对象拥有一个原型对象"
      );
      // 有点类似OOP中继承的概念
      function doSomeThing(name, age) {
        this.name = name;
        this.age = age;
      }
      console.log(doSomeThing.prototype);

      // 添加属性到原型
      doSomeThing.prototype.name = "dong";
      console.log(doSomeThing.prototype);

      let doSome = new doSomeThing();
      doSome.prop = "some value";
      console.log(doSome);
      console.log("doSome.prop:      " + doSome.prop);
      console.log("doSome.foo:       " + doSome.name);
      console.log("doSomething.prop:           " + doSomeThing.prop);
      console.log("doSomething.foo:            " + doSomeThing.name);
      console.log("doSomething.prototype.prop: " + doSomeThing.prototype.prop);
      console.log("doSomething.prototype.foo:  " + doSomeThing.prototype.name);

      log("对象引用");
      let doSome1 = doSome;
      doSome1.prop = "many value";
      console.log(doSome);
      console.log(doSome1);

      log("对象拷贝");
      let doSome2 = Object.create(doSome);
      doSome2.prop = "many value1";
      console.log(doSome);
      console.log(doSome2);

      log("构造器, 可以通过对象构造器函数继承属性");
      doSome3 = new doSome2.constructor("xixi", 100);
      console.log(doSome3);

      function log(str) {
        console.log("----------------" + str + "----------------");
      }
    </script>
  </head>
  <body></body>
</html>
